package com.multitrack.main;

import static com.multitrack.datatype.SocketMessage.IS_MEMBER;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;

import com.multitrack.datatype.STATE;
import com.multitrack.datatype.SocketMessage;
import com.multitrack.util.CloseAlertDialog;
import com.multitrack.util.CommunicationManager;
import com.multitrack.util.SQLite;
import com.multitrack.util.ThreadManager;

// 처음 화면 시작
public class IntroActivity extends Activity {
	static String TAG = "IntroActivity";

	private SQLite mHelper;
	private String myId;

	final long sleepTime = 1000;

	SleepThread sleepThread;
	CommunicationManager isMemberThread;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intro_activity);

		TelephonyManager telephony = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		mHelper = new SQLite(this);
		if( mHelper.dbGetMyId() == null) {
			mHelper.dbSetMyId(telephony.getLine1Number());
		}
		myId = mHelper.dbGetMyId();

		sleepThread = new SleepThread(sleepTime);
		ThreadManager.add(sleepThread);
		sleepThread.start();

		excuteCheck();
	}

	private void excuteCheck() {
		if (ThreadManager.isInThread(isMemberThread) == false) {
			isMemberThread = new CommunicationManager();
			ThreadManager.excute(isMemberThread, mHandler, IS_MEMBER, myId);
		}
	}

	class SleepThread extends Thread {
		long time;

		SleepThread(long time) {
			this.time = time;
		}

		@SuppressWarnings("static-access")
		public void run() {
			try {
				this.sleep(time);
			} catch (Exception e) {
				e.printStackTrace();
			}
			ThreadManager.remove(currentThread());
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHelper.close();
	}

	Handler mHandler = new Handler() {
		Intent intent = null;
		SocketMessage resultMessage;
		STATE resultState;

		@Override
		public void handleMessage(Message msg) {

			resultState = STATE.getEnum((msg.what));
			resultMessage = (SocketMessage) msg.obj;

			switch (resultState) {
			case SOCKET_SUCCESS:
				uponSuccess();
				break;
			case SOCKET_FAIL:
				uponFail();
				break;
			case SOCKET_EXCEPTION:
				uponException();
				break;
			}
		}

		private void uponSuccess() {

			switch (resultMessage) {
			case ID_FOUND:

				intent = new Intent(IntroActivity.this, TabMenuActivity.class);
				intent.putExtra(STATE.MESSAGE, resultState);
				startActivity(intent);
				finish();
				break;

			case ID_NOT_FOUND:
				intent = new Intent(IntroActivity.this, AgreeActivity.class);
				startActivity(intent);
				finish();
				break;

			}
		}

		private void uponFail() {
			OnClickListener listener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(IntroActivity.this,
							TabMenuActivity.class);
					intent.putExtra(STATE.MESSAGE, resultState);
					startActivity(intent);
					finish();
				}
			};

			String message = "서버 접속에 실패하였습니니다.\nEvent가 비 활성화 됩니다.";
			new CloseAlertDialog(IntroActivity.this, R.string.notice, message,
					listener).show();

		}

		private void uponException() {
			OnClickListener nListener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					excuteCheck();
					finish();
				}
			};

			OnClickListener pListener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					uponFail();
				}
			};

			String message = "다시 연결하시겠습니까?\n(R.string)";
			new CloseAlertDialog(IntroActivity.this, R.string.notice, message,
					nListener, pListener).show();

		}
	};
}
